#include <windows.h>
#include "MSR_NuiApi.h"
#include <stdio.h>

//Events ...
HANDLE m_hNextDepthFrameEvent;
HANDLE m_hNextVideoFrameEvent;
HANDLE m_hNextSkeletonEvent;

//EventHandle
HANDLE m_pVideoStreamHandle;
HANDLE m_pDepthStreamHandle;

// thread handling
HANDLE m_hThNuiProcess;
HANDLE m_hEvNuiProcessStop;


int main(int argc, char* argv){

	HRESULT    hr;
 m_hNextDepthFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
 m_hNextVideoFrameEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
 m_hNextSkeletonEvent = CreateEvent( NULL, TRUE, FALSE, NULL );

 hr = NuiInitialize( 
  NUI_INITIALIZE_FLAG_USES_DEPTH_AND_PLAYER_INDEX | NUI_INITIALIZE_FLAG_USES_SKELETON | NUI_INITIALIZE_FLAG_USES_COLOR);
 if( FAILED( hr ) ) printf("Initialize verkackt \ n");
 
	hr = NuiSkeletonTrackingEnable( m_hNextSkeletonEvent, 0 );
 if( FAILED( hr ) ) printf("TrackingEnable verkackt \n");
 
	 hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_COLOR, NUI_IMAGE_RESOLUTION_640x480,
							 0, 2, m_hNextVideoFrameEvent, &m_pVideoStreamHandle );
 if( FAILED( hr ) ) printf("VideoStreamHandle verkackt \n");
 

 hr = NuiImageStreamOpen(NUI_IMAGE_TYPE_DEPTH_AND_PLAYER_INDEX, NUI_IMAGE_RESOLUTION_320x240,
							0, 2, m_hNextDepthFrameEvent, &m_pDepthStreamHandle );
 if( FAILED( hr ) ) printf("DepthStreamHanle verkackt \n");
 
 HANDLE	hEvents[4];
 int		nEventIdx;

	// Configure events to be listened on
 hEvents[0] = m_hEvNuiProcessStop;
 hEvents[1] = m_hNextDepthFrameEvent;
 hEvents[2] = m_hNextVideoFrameEvent;
 hEvents[3] = m_hNextSkeletonEvent;
	
	// Main thread loop
 while (1)
{
  const NUI_IMAGE_FRAME * pImageFrame = NULL;
  HRESULT hr = NuiImageStreamGetNextFrame(m_pVideoStreamHandle, 200, &pImageFrame);
  if( hr == E_NUI_FRAME_NO_DATA)
  {
    // No stream frame was found after waiting for
    // requested amount. Continue and try again (or, you could
    // also choose to bail out from function here).
    continue;
  }
  else if (FAILED(hr))
  {
    break;
  }

  // Your code to deal with data from pImageFrame
}
 return hr;
}